package org.tmad.ocms.service;



import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import org.tmad.ocms.dao.CaseDAO;
import org.tmad.ocms.exceptions.ApplicationException;
import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.mail.EmailSender;
import org.tmad.ocms.mail.EmailTemplates;
import org.tmad.ocms.mail.Template;
import org.tmad.ocms.model.Case;

public class CaseService {
	private CaseDAO cDAO = null;
	private static Logger logger = LogFactory.getLogger();
	
	
	
	public boolean caseUser(Case user) throws ApplicationException{
		
		boolean result =false;
		cDAO = new CaseDAO();
		
		try{
		result = cDAO.caseregister(user);
		
		}catch(Exception e){
			logger.error("error while registration ", e);
			throw new ApplicationException("Unable to register" + e.getMessage());
		}
		
		// writer.write("Registration is successful");
		
		sendCaseRegistrationEmail(user.getCaseType(), user.getHelpType(), user.getNeedyName(),user.getDescription(),user.getAddress(),user.getEmailId());
	    
		return result;
	}
	

	/**
	 * Sends out email on successful registration
	 * 
	 * @param email
	 *            the email id of the user.
	 * @param userName
	 *            the user name.
	 * @param firstName
	 *            first name of the user.
	 * @param password
	 *            password of the user. Remember password is always stored in
	 *            encrypted format on database. Only at the time of
	 *            registration, password is in plain text.
	 */

	public void sendCaseRegistrationEmail(String caseType,String helpType,String needyName,String description,String address,String emailId ) {
		
		Map<String, String> valuesMap = new HashMap<String, String>();
		valuesMap.put(EmailTemplates.caseType,caseType);
		valuesMap.put(EmailTemplates.helpType,helpType);
		valuesMap.put(EmailTemplates.needyName,needyName);
		valuesMap.put(EmailTemplates.description,description);
		valuesMap.put(EmailTemplates.address,address);
		valuesMap.put(EmailTemplates.emailId,emailId);
		
			
		String message = EmailTemplates.getEmailMessage(Template.CaseRegistration,valuesMap);
		EmailSender.sendMailWithSSL("Registration successful", message,emailId);

	}
}
